use egui::{Ui, RichText, Color32, Rounding, Frame, Vec2, Stroke};
use crate::ui::theme::AppTheme;
use eframe::egui::{self, Response};

// 卡片组件
pub fn card_frame(theme: &AppTheme, is_selected: impl Into<Option<bool>>) -> Frame {
    let padding = match is_selected.into() {
        Some(true) => 16.0,  // 选中状态使用较大内边距
        Some(false) => 12.0, // 未选中状态使用较小内边距
        None => 16.0,        // 默认内边距
    };
    
    Frame::none()
        .fill(theme.card_background)
        .rounding(Rounding::same(12.0))
        .inner_margin(padding)
        .shadow(egui::epaint::Shadow {
            extrusion: 2.0,
            color: Color32::from_black_alpha(40),
        })
}

// 标题组件
pub fn section_title(ui: &mut Ui, theme: &AppTheme, text: &str) {
    ui.add_space(8.0);
    ui.heading(RichText::new(text).color(theme.primary).size(20.0));
    ui.add_space(12.0);
}

// 带标签的输入框组件
pub fn labeled_text_edit(ui: &mut Ui, theme: &AppTheme, label: &str, value: &mut String) {
    ui.horizontal(|ui| {
        let label_width = 120.0;
        ui.add_sized(
            [label_width, 24.0],
            egui::Label::new(RichText::new(label).color(theme.text_secondary))
        );
        ui.text_edit_singleline(value);
    });
}

// 带标签的复选框组件
pub fn labeled_checkbox(ui: &mut Ui, theme: &AppTheme, label: &str, checked: &mut bool) {
    ui.horizontal(|ui| {
        let label_width = 120.0;
        ui.add_sized(
            [label_width, 24.0],
            egui::Label::new(RichText::new(label).color(theme.text_secondary))
        );
        ui.checkbox(checked, "");
    });
}

// 带标签的数字输入框组件
pub fn labeled_u16_edit(ui: &mut Ui, theme: &AppTheme, label: &str, value: &mut u16) {
    ui.horizontal(|ui| {
        let label_width = 120.0;
        ui.add_sized(
            [label_width, 24.0],
            egui::Label::new(RichText::new(label).color(theme.text_secondary))
        );
        ui.add(egui::DragValue::new(value).clamp_range(1..=65535));
    });
}

// 圆形状态指示器
pub fn status_circle(ui: &mut Ui, radius: f32, is_active: bool, theme: &AppTheme) {
    let (rect, _) = ui.allocate_exact_size(
        Vec2::new(radius * 2.0, radius * 2.0),
        egui::Sense::hover(),
    );
    
    // 状态颜色
    let status_color = if is_active {
        theme.success
    } else {
        theme.error
    };
    
    // 绘制圆形指示器
    ui.painter().circle(
        rect.center(),
        radius,
        status_color,
        Stroke::new(2.0, theme.card_background),
    );
}

// 自定义按钮
pub fn primary_button(theme: &AppTheme, text: &str) -> egui::Button {
    egui::Button::new(
        RichText::new(text)
            .size(16.0)
            .color(Color32::WHITE)
    )
    .min_size(Vec2::new(120.0, 32.0))
    .rounding(Rounding::same(6.0))
    .fill(theme.primary)
}

pub fn secondary_button(theme: &AppTheme, text: &str) -> egui::Button {
    egui::Button::new(
        RichText::new(text)
            .size(16.0)
            .color(theme.text)
    )
    .min_size(Vec2::new(100.0, 32.0))
    .rounding(Rounding::same(6.0))
    .fill(theme.secondary.gamma_multiply(0.7))
}

pub fn danger_button(theme: &AppTheme, text: &str) -> egui::Button {
    egui::Button::new(
        RichText::new(text)
            .size(16.0)
            .color(Color32::WHITE)
    )
    .min_size(Vec2::new(100.0, 32.0))
    .rounding(Rounding::same(6.0))
    .fill(theme.error)
}

/// 创建一个自定义按钮
pub fn make_button(
    ui: &mut Ui, 
    text: &str, 
    color: egui::Color32, 
    size: [f32; 2], 
    text_size: f32, 
    rounding: f32
) -> Response {
    ui.add_sized(
        Vec2::new(size[0], size[1]),
        egui::Button::new(
            RichText::new(text)
                .size(text_size)
                .color(egui::Color32::WHITE)
        )
        .fill(color)
        .rounding(Rounding::same(rounding))
    )
} 